import {
  UserPhotoResponse,
  UserProfile,
  UserProfileResponse,
  UserResponse,
} from '@/types/data'
import { RootThunkAction } from '@/types/store'
import http from '@/utils/http'

export const getUser = (): RootThunkAction => {
  return async (dispatch) => {
    const res = await http.get<UserResponse>('/user')
    // console.log(res.data.data)

    dispatch({ type: 'profile/getUser', payload: res.data.data })
  }
}

export const getUserProfile = (): RootThunkAction => {
  return async (dispatch) => {
    const res = await http.get<UserProfileResponse>('/user/profile')
    console.log(res)

    dispatch({ type: 'profile/getUserProfile', payload: res.data.data })
  }
}

export const updateProfile = (
  userProfile: Partial<UserProfile>
): RootThunkAction => {
  return async (dispatch) => {
    await http.patch('/user/profile', userProfile)

    dispatch({ type: 'profile/update', payload: userProfile })
  }
}

export const updatePhoto = (file: FormData): RootThunkAction => {
  return async (dispatch) => {
    const res = await http.patch<UserPhotoResponse>('/user/photo', file)
    console.log(res)

    dispatch({ type: 'profile/photo', payload: res.data.data.photo })
  }
}
